goog.provide('tipfy.editor.plugins.SimpleTag');

goog.require('goog.dom');
goog.require('goog.editor.Plugin');
goog.require('goog.editor.range');


/**
 * Plugin to insert 'Hello World!' into an editable field.
 * @constructor
 * @extends {goog.editor.Plugin}
 */
tipfy.editor.plugins.SimpleTag = function(command, tag) {
  goog.editor.Plugin.call(this);
  this.command_ = command;
  this.tag_ = tag;
  this.setAutoDispose(true);
};
goog.inherits(tipfy.editor.plugins.SimpleTag, goog.editor.Plugin);


/** @inheritDoc */
tipfy.editor.plugins.SimpleTag.prototype.isSupportedCommand = function(
    command) {
  return command == this.command_;
};


/**
 * Executes a command. Does not fire any BEFORECHANGE, CHANGE, or
 * SELECTIONCHANGE events (these are handled by the super class implementation
 * of {@code execCommand}.
 * @param {string} command Command to execute.
 * @override
 * @protected
 */
tipfy.editor.plugins.SimpleTag.prototype.execCommandInternal = function(
    command) {
  var domHelper = this.fieldObject.getEditableDomHelper();
  var range = this.fieldObject.getRange();
  if(!range.isCollapsed()) {
      var newNode = domHelper.createDom(this.tag_, null, range.getValidHtml());
      range.replaceContentsWithNode(newNode, false);
  }
};
